$(function () {
    let id = location.search.split("=")[1]
    cate();
    newComment();
    focusFollow();
    hotList();

    // let index = 0;
    let q = {
        key: '',
        type: id,
        page: 1,
        perpage: 6,
    }
    list(q);
    // 搜索
    $(".search_btn").on("click", function () {
        let val = $(".search_txt").val();
        q.key = val;
        $(".common_news_list").remove();
        list(q);
    })


    // 列表渲染
    function list(q) {
        $.ajax({
            url: '/index/search',
            type: 'get',
            data: q,
            success: (res) => {
                res.data.data.forEach(item => {
                    // img图片替换
                    item.content = item.content.replace(/&lt;/g, '<')
                    item.content = item.content.replace(/&gt;/g, '>')
                    item.content = item.content.replace(/&nbsp;/g, ' ')
                    item.content = item.content.replace(/&quot;/g, '"')
                    item.content = item.content.replace(/%3A/g, ':')
                    item.content = item.content.replace(/%2F/g, '/')

                })
                console.log(res.data.data);

                // $.each(res.data.data, function (i, n) {
                //     console.log(n.content.replace(/<\/?.+?\/?>/g, ''));

                // })
                let htmlStr = template("new_rti", {
                    list: res.data.data
                })
                // 标题
                const {
                    category
                } = res.data.data[0];
                $(".list_title h3").html(category);

                // 分页
                $("#pagination").pagination({

                    currentPage: q.page,
                    totalPage: Math.ceil(res.data.totalCount / q.perpage),
                    callback: function (current) {
                        console.log(current);

                        $(".common_news_list").remove();

                        q.page = current;

                        list(q);


                    }

                });
                $("#pagination").before($(htmlStr))

            }
        })
    }



    // 分类 导航栏
    function cate() {
        $.ajax({
            url: '/index/category',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);
                let htmlStr = template("cate", {
                    list: res.data
                })
                $(".level_two").html(htmlStr)
                $(".left_menu").html(htmlStr)
            }
        })
    }

    // 一周热门排行
    function hotList() {
        $.ajax({
            url: '/index/rank',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);

                let htmlStr = template("hot_list", {
                    list: res.data
                })
                $(".hotrank_list").html(htmlStr)
                $(".hotrank_list li").eq(0).find("span").addClass("first")
                $(".hotrank_list li").eq(1).find("span").addClass("second")
                $(".hotrank_list li").eq(2).find("span").addClass("third")
            }
        })
    }

    // 最新评论
    function newComment() {
        $.ajax({
            url: '/index/latest_comment',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);
                let htmlStr = template("new_comment", {
                    list: res.data
                })
                $(".comment_list").html(htmlStr)
            }
        })
    }

    // 焦点关注
    function focusFollow() {
        $.ajax({
            url: '/index/attention',
            type: 'get',
            data: {},
            success: (res) => {
                // console.log(res);
                let htmlStr = template("focus_follow", res

                )
                $(".guanzhu_list").html(htmlStr)
            }
        })
    }
})